<template>
  <div>
    <el-dialog
      :title="title"
      :closeOnClickModal="false"
      :visible.sync="show"
      width="650px">
      <el-form 
        label-position="right" 
        :model="selectTable"
        ref="addForm" 
        label-width="100px" 
        :inline="false"
        autocomplete="off"
        >
        <el-row>
          <el-col v-if="selectTable.method==='edit'">
            <el-form-item label="复制网址">
              <el-input v-model="selectTable.manuscriptPath" maxLength="100" style="width: 70%;" disabled></el-input>
              <el-button icon="el-icon-tickets" v-clipboard:copy='selectTable.manuscriptPath' v-clipboard:success='clipboardSuccess'>复制</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="channelName" :rules="[rule[0]]">
              <el-input placeholder="请输入" v-model="selectTable.channelName" maxLength="100"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="别名" prop="templetName" :rules="[rule[0]]">
              <el-input placeholder="请输入" v-model="selectTable.templetName" maxLength="30">
                 <template slot="append">.html</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="PC模板" prop="templetId">
              <tplselect v-model="selectTable.templetId"></tplselect>
            </el-form-item>
          </el-col>
          <el-col>
            <el-form-item label="WAP模板" prop="mTempletId">
              <tplselect v-model="selectTable.mTempletId"></tplselect>
            </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="SEO标题" prop="seoHeadline">
          <el-input placeholder="请输入" v-model="selectTable.seoHeadline" maxLength="100"></el-input>
        </el-form-item>
        <el-form-item label="SEO关键字"  prop="seoKeyword">
          <el-input placeholder="请输入" v-model="selectTable.seoKeyword" maxLength="100"></el-input>
        </el-form-item>
        <el-form-item label="SEO描述" prop="seoDepict">
          <el-input type="textarea" placeholder="请输入" v-model="selectTable.seoDepict" maxLength="100"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="comment">
          <el-input placeholder="请输入备注" v-model="selectTable.comment" maxLength="100"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="pload">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { channelSave, channelModify } from '@/utils/api'
import tplselect from '../sysTpl/tplSelect'
import clipboard from '@/directive/clipboard/index.js' // use clipboard by v-directive
export default {
  directives: {
    clipboard
  },
  components: {
    tplselect
  },
  props: {
    data: Object
  },
  created() {
    this.selectTable = this.JSONs(this.data)
  },
  watch: {
    show(newVal) {
      this.$parent.edit = false
    }
  },
  computed: {
    title() {
      if (this.selectTable.method === 'edit') {
        return '修改'
      } else if (this.selectTable.method === 'add') {
        return '添加'
      }
    }
  },
  data() {
    return {
      pload: false,
      rule: [
        { required: true, message: '必填项', trigger: 'change' }
      ],
      show: true,
      selectTable: {}
    }
  },
  methods: {
    clipboardSuccess() {
      this.$message({
        message: '复制成功',
        type: 'success',
        duration: 1500
      })
    },
    async submitForm(formName) {
      await this.$refs[formName].validate()
      try {
        this.pload = true
        this.selectTable.channel = 1
        if (this.selectTable.method === 'edit') {
          await channelModify(this.selectTable)
        } else if (this.selectTable.method === 'add') {
          await channelSave(this.selectTable)
        }
        this.$notify({ title: '操作成功', type: 'success', duration: 5000 })
        this.$parent.edit = false
        this.$emit('getData')
      } catch (e) {
        this.pload = false
      }
    }
  }
}
</script>